import random
import socket
import json
from time import sleep
# 定义起止点线段
def line(x1,y1,x2,y2):
    return [x1,y1],[x2,y2]

line_start,line_end = line(1,1,65,89)
# 构造消息
def create_msg():
    x = random.uniform(line_start[0],line_end[0])
    y = random.uniform(line_start[1],line_end[1])
    data = {
    "hid":'0001',
    "location":{
        "x":x,
        "y":y
    },
    "timestamp":1234567890,
    "tunnel_layer":'101'
    }

    send_msg = json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))
    return send_msg

# 创建UDP套接字
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# 目标地址和端口
dest_addr = ('192.168.220.100', 10005)
dest_addr_bak = ('192.168.220.100', 10006)


# 循环
while(True):
    sleep(1)
    send_msg = create_msg()
    # 发送数据
    udp_socket.sendto(send_msg.encode('utf-8'), dest_addr)
    udp_socket.sendto(send_msg.encode('utf-8'), dest_addr_bak)

